<?

class LoggerDatabase
{
    private static $__link;
    private static $__error;

    public function __construct() 
    {
        $this->__link = mysql_connect(LoggerConfig::$mysqlHost, LoggerConfig::$mysqlLogin, LoggerConfig::$mysqlPassword);
        if (!$this->__link) {
            trigger_error('Database Error: '.mysql_error(), E_USER_ERROR);
        }
        mysql_selectdb(LoggerConfig::$mysqlDatabase, $this->__link);
        mysql_query('SET NAMES UTF8', $this->__link);
        mysql_query('SET time_zone = \'+03:00\'', $this->__link);
    }

    public function Disconnect() 
    {
        if ($this->__link)
        {
            mysql_close($this->__link);
            $this->__link = null;
        }
    }

    public function Error()
    {
        return $this->__error;
    }

    public function Query($query)
    {
        $this->__error = '';
        $query = str_replace('[PREF]', LoggerConfig::$mysqlTablePrefix, $query);
        $result = mysql_query($query, $this->__link);
        if (!$result) {
            $this->__error = mysql_error($this->__link);
            $this->__error .= ' [' . $query . '] ';
            return false;
        }
        return $result;
    }

    public function FetchAssoc($result)
    {
        return mysql_fetch_assoc($result);
    }

    public function InsertId()
    {
        return mysql_insert_id($this->__link);
    }

    public function AffectedRows()
    {
        return mysql_affected_rows($this->__link);
    }

    public function NumRows($result)
    {
        return mysql_num_rows($result);
    }

    public function FreeResult($result)
    {
        return mysql_freeresult($result);
    }
}